// Learn cc.Class:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/class/index.html
// Learn Attribute:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/reference/attributes/index.html
// Learn life-cycle callbacks:
//  - [Chinese] http://www.cocos.com/docs/creator/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/editors_and_tools/creator-chapters/scripting/life-cycle-callbacks/index.html
var obj = require('SceneObj');
var comps = require('ComponentDifine');

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad() {
        if (!cc.sys.isNative && cc.sys.isMobile) {
            var cvs = this.node.getComponent(cc.Canvas);
            cvs.fitHeight = true;
            cvs.fitWidth = true;
        }

        var compAssets = [
            comps.roomPlayerInfo,
            comps.roomReadyPlay,
            comps.roomReadySign,
            comps.roomMjTiles];

        App.playAssets = {};

        for (let i = 0; i < compAssets.length; i++) {
            cc.loader.loadRes(compAssets[i], (err, prefab) => {
                if (err) {
                    cc.error(err);
                    return;
                }
                App.playAssets[compAssets[i]] = prefab;
                console.log(App.playAssets[compAssets[i]]);
            });
        }
        // cc.view.enableRetina(cc.sys.os === cc.sys.OS_IOS ? true : false);
        // cc.view.adjustViewPort(true);
        // cc.view.setOrientation(cc.ORIENTATION_LANDSCAPE);
        // // FIXED_WIDTH  SHOW_ALL
        // if (cc.sys.os === cc.sys.OS_ANDROID || cc.sys.os == cc.sys.OS_IOS) {
        //     cc.view.setDesignResolutionSize(1136, 640, cc.ResolutionPolicy.FIXED_HEIGHT);
        // } else {
        //     // 浏览器
        //     cc.view.setDesignResolutionSize(1136, 640, cc.ResolutionPolicy.SHOW_ALL);
        // }

        // cc.view.resizeWithBrowserSize(true);
        // cc.view.enableAutoFullScreen(true);
        // window.addEventListener('orientationchange', function (event) {
        //     var gap = (cc.winSize.width - display.width) / 2;
        //     var scene = cc.director.getRunningScene();
        //     scene.x += gap;

        //     display.width = cc.winSize.width;
        //     display.height = cc.winSize.height;
        //     display.cx = display.width / 2;
        //     display.cy = display.height / 2;
        // });

        this._splash = cc.find("Canvas/splash");
        this._splash.active = true;
        this.initialise();

        var delayAction = cc.fadeIn(1.0);
        var fadeOutAction = cc.fadeOut(1.0);
        var callFunc = cc.callFunc(
            //cc.director.loadScene("Main")
            this.startPreloading()
        )
        var seqAction = cc.sequence(delayAction, fadeOutAction, callFunc);

        this._splash.runAction(seqAction);
    },

    // start () {},

    initialise: function () {
        cc.h5App = {};
        // var Utils = require("Utils");
        // cc.webApp.utils = new Utils();

        //cc.h5App.ws = require("Connection")
    },

    startPreloading: function () {
        this._stateStr = "正在加载资源，请稍候";
        this._isLoading = true;
        var self = this;

        cc.loader.onProgress = function (completedCount, totalCount, item) {
            //console.log("completedCount:" + completedCount + ",totalCount:" + totalCount );
            if (self._isLoading) {
                self._progress = completedCount / totalCount;
            }
        };

        cc.loader.loadResDir("textures", function (err, textures) {
            if (err) {
                cc.error(err);
                return;
            }
            self.onLoadComplete();
        });
    },

    onLoadComplete: function () {
        this._isLoading = false;
        this._stateStr = "准备登陆";
        cc.director.loadScene("Hall");
        cc.loader.onComplete = null;
    },
    // update (dt) {},
});
